package com.lix.service.workflow.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lix.base.BaseResult;
import com.lix.enums.BusinessStatusEnum;
import com.lix.mapper.BusinessStatusMapper;
import com.lix.pojo.model.BusinessStatus;
import com.lix.service.workflow.IBusinessStatusService;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class BusinessStatusServiceImpl extends ServiceImpl<BusinessStatusMapper, BusinessStatus> implements IBusinessStatusService {

    /**
     * 新增
     *
     * @param businessKey 业务id
     * @return
     */
    @Override
    public int add(String businessKey) {
        BusinessStatus bs = new BusinessStatus();
        bs.setBusinessKey(businessKey);
        bs.setStatus(BusinessStatusEnum.WAIT.getCode());
        return baseMapper.insert(bs);
    }

    @Override
    public BaseResult updateState(String businessKey, BusinessStatusEnum stateEnum, String procInstId) {
        BusinessStatus bs = baseMapper.selectById(businessKey);
        // 状态
        bs.setStatus(stateEnum.getCode());
        bs.setUpdateDate(new Date());
        // 值为null,不会更新此字段
        if (procInstId != null) {
            bs.setProcessInstanceId(procInstId);
        }
        baseMapper.updateById(bs);
        return BaseResult.ok();
    }

    @Override
    public BaseResult updateState(String businessKey, BusinessStatusEnum stateEnum) {
        return updateState(businessKey, stateEnum, null);
    }
}